package com.example.exampleOS;

import com.example.exampleOS.slice.ClockCardSlice;
import com.example.exampleOS.slice.LocationSlice;
import com.example.exampleOS.slice.MainAbilitySlice;
import com.example.exampleOS.slice.WebViewSlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.orm.OrmContext;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbility extends Ability implements IAbilityContinuation {
//    钟表 start
private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "com.huawei.cookbooks.MainAbility");
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private static final int DEFAULT_DIMENSION_2X4 = 3;
    private static final String EMPTY_STRING = "";
    private static final int INVALID_FORM_ID = -1;
    private long formId;
    private ProviderFormInfo formInfo;
    // 钟表 end
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        super.setMainRoute(MainAbilitySlice.class.getName());
        addActionRoute("action.ui.webview", WebViewSlice.class.getName());
        addActionRoute("action.system.location", LocationSlice.class.getName());
        // 启动TimerAbility
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ClockServiceAbility.class)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
//        super.setMainRoute(ClockCardSlice.class.getName());

        //        设备迁移 start
        /**
         * 权限申请
         */
        requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"},0);
        //        设备迁移 start
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        if (intent == null) {
            return new ProviderFormInfo();
        }
        // 获取卡片id
        formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        } else {
            return new ProviderFormInfo();
        }
        // 获取卡片名称
        String formName = EMPTY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }
        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }
        int layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_2;
        if (dimension == DEFAULT_DIMENSION_2X4) {
            layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_4;
        }
        formInfo = new ProviderFormInfo(layoutId, this);

        return formInfo;
    }

    @Override
    protected void onDeleteForm(long formId) {
        super.onDeleteForm(formId);

    }
    //设备迁移 start
    @Override
    public boolean onStartContinuation() {

        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
    //设备迁移 end
}
